$(function(){
    getCookieRecord()
})

// 用于判断注册的来源是否来自于广告链接
function utmAdver() {
	let utmAdver = {}
	utmAdver.utmSource = decodeURI(escape(getParam('utm_source')))
	utmAdver.utmMedium = decodeURI(escape(getParam('utm_medium')));
	utmAdver.utmCampaign = decodeURI(escape(getParam('utm_campaign')));
	utmAdver.utmTerm = decodeURI(escape(getParam('utm_term')));
    if(!(utmAdver.utmSource || utmAdver.utmMedium || utmAdver.utmCampaign || utmAdver.utmTerm)) return
	localStorage.setItem('utmAdver', JSON.stringify(utmAdver))
}
function getCookieRecord(){
	var ad_code_link=getParam('ad_code');
	var ad_code_cookie= $.cookie('ad_code');
	var wd_link=getParamReChinese('wd');
	var wd_cookie=$.cookie('wd');
	var bd_vid_link= getParam('bd_vid');
	var bd_vid_cookie= $.cookie('bd_vid');
	var qhclickid_link= getParam('qhclickid');
	var qhclickid_cookie= $.cookie('qhclickid');
	var utm_source = getParam('utm_source');
	
	if(ad_code_cookie){
		$.cookie('ad_code',ad_code_cookie, { path:'/',expires: 1 })	
		$.cookie('ad_referer',$.cookie('ad_referer'), { path:'/',expires: 1 })	
	}else if(ad_code_link){	
		$.cookie('ad_code',ad_code_link, { path:'/',expires: 1 })	
		$.cookie('ad_referer',window.location.href||"", { path:'/',expires: 1 })	
	} else if(utm_source) { //有utm_source就设置ad_referer
		$.cookie('ad_referer',window.location.href||"", { path:'/',expires: 1 })	
	}

	if(wd_cookie){
		$.cookie('wd',wd_cookie, { path:'/',expires: 1 })	
	}else if(wd_link&&ad_code_link&&!ad_code_cookie){
		$.cookie('wd',wd_link, { path:'/',expires: 1 })	
	}
	
	if(bd_vid_cookie){
		$.cookie('bd_vid',bd_vid_cookie, { path:'/',expires: 1 })	
	
	}else if(bd_vid_link){
		$.cookie('bd_vid',bd_vid_link, { path:'/',expires: 1 })
		
	}

	if(qhclickid_cookie){
		$.cookie('qhclickid',qhclickid_cookie, { path:'/',expires: 1 })	
	}else if(qhclickid_link){
		$.cookie('qhclickid',qhclickid_link, { path:'/',expires: 1 })
	}
	utmAdver()
}

function getParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return '';
}
function getParamReChinese(item) {
	var reg = new RegExp("(^|&)" + item + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substring(1).match(reg);
	if (r != null) return decodeURI(r[2]);
	return '';
}